package edu.ccut.saturn.studio.menuitem.wizard;

import java.io.File;
import java.util.Map;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;


import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;

public class MenuitemWizardPage extends SaturnWizardPage {

	private static final String MENU_ELEMENT_NAME = "menu"; //$NON-NLS-1$
	public static final String MENU_FILE_NAME = "name"; //$NON-NLS-1$
	public static final String MENU_ROOTNODE_NAME = "rootNode"; //$NON-NLS-1$
	private Text menuProjectNameText;
	private Text menuNameText;
	Map<String, IProject> map = null;
	private static final String CON_FILE_NAME = "/src/menu.config"; //$NON-NLS-1$
	
	public MenuitemWizardPage() {
		setTitle(Messages.getString("MenuitemWizardPage.menuconfig")); //$NON-NLS-1$
		setDescription(""); //$NON-NLS-1$
		
	}

	@Override
	public void initalValues(Map<String, Object> values) {
		values.put(MENU_FILE_NAME, this.menuNameText.getText().trim());
		values.put(MENU_ROOTNODE_NAME, Messages.getString("MenuitemWizardPage.rootnode")); //$NON-NLS-1$
	}


	
	public void createControl(Composite parent) {
			Composite container = new Composite(parent, SWT.NULL);
			final GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 3;
			container.setLayout(gridLayout);
			

			final Label label = new Label(container, SWT.NONE);
			label.setText(Messages.getString("MenuitemWizardPage.name")); //$NON-NLS-1$

			menuNameText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
			final GridData gd_menuNameText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
			gd_menuNameText.widthHint = 431;
			menuNameText.setLayoutData(gd_menuNameText);
			menuNameText.setText(MENU_ELEMENT_NAME);
			setControl(container);
			validate();
	}
	
	public String getPath() {
		return menuProjectNameText.getText();
	}

	/**
	 * 验证是否存在菜单管理的文件
	 */
		public  boolean validate() {
			if (!validateProjectName()) {
				setErrorMessage(Messages.getString("MenuitemWizardPage.theprojectexistconfigfile"));  //$NON-NLS-1$
				setPageComplete(false);
				return false;
			}
			setErrorMessage(null);
			setPageComplete(true);
			
			return true;
		}
		
		private boolean validateProjectName() {
			IProject iProject = SaturnProjectUtil.getProject(super.getSelection());
			String path = iProject.getLocation() + CON_FILE_NAME;
			IPath filePath = new Path(path);
			File file = filePath.toFile();
			if (file.exists()) {
				return false;
			}
			return true;
		}
		
}
